home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / ncsat.cpt / Telnet2.5 final / tcpip / mactools.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-08  |  11.2 KB  |  451 lines

  1. /*
  2. *  mactools.c                by Gaige B. Paulsen
  3. *
  4. *  adapted from pctools.c      by Tim Krauskopf
  5. ****************************************************************************
  6. *                                                                          *
  7. *      part of:                                                            *
  8. *      TCP/UDP/ICMP/IP Network kernel for NCSA Telnet                      *
  9. *      by Tim Krauskopf                                                    *
  10. *                                                                          *
  11. *      National Center for Supercomputing Applications                     *
  12. *      152 Computing Applications Building                                 *
  13. *      605 E. Springfield Ave.                                             *
  14. *      Champaign, IL  61820                                                *
  15. *                                                                          *
  16. *                                                                          *
  17. ****************************************************************************
  18. *
  19. *  those generic tool-type things that only work on Macs.
  20. *  includes all hardware-level calls to Ethernet that are unique to the Mac
  21. *
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <String.h>
  26.  
  27. #include <Dialogs.h>
  28. #include <Files.h>
  29. #include <AppleTalk.h>
  30. #include <Events.h>
  31. #include <Memory.h>
  32.  
  33. #include "protocol.h"
  34. #include "data.h"
  35. #include "fastnet.h"
  36. #include "configrec.h"
  37. #include "ethertalk.h"
  38. #include "atalk.h"                /* Dlayer to atalk conversion */
  39. #include "dlayer.h"
  40. #include "ip.h"
  41. #include "ether.h"
  42. #include "elistener.h"
  43. #include "listener.h"
  44. #include "maclook.h"
  45. #include "slip.h"                /* BYU 2.4.16 MPW */
  46.  
  47. #ifdef MPW
  48. #define    movmem(y,x,z)    memcpy(x,y,z)
  49. #endif
  50.  
  51. extern short slip_connection;                    /* BYU 2.4.15 */
  52.  
  53. /*
  54.  * THE ALL-POWERFUL VARIABLE !!!!!!!!!!
  55.  */
  56.  
  57. int EtherNet
  58.   /*
  59.     specifies hardware interface to use.
  60.     Values are
  61.         -100    -- no hardware, serial port only                     BYU 2.4.15
  62.         -99        -- no hardware, using MacTCP (set up in userd.c)
  63.         -2        -- FastNet
  64.         -1        -- Kinetics Ether SC or Ether SE
  65.         0        -- LocalTalk ("built-in")
  66.         1        -- 3Com EtherLink/NB (or Apple EtherTalk)
  67.         9 .. 14 -- EtherLink/NB in specified NuBus slot
  68.   */
  69.     = 0;        /* default to LocalTalk */
  70.  
  71.  
  72.  
  73. /**********************************************************************/
  74. /*  netarpme
  75. *   send an arp to my address.  arpinterpret will notice any response.
  76. *   Checks for adapters which receive their own broadcast packets.
  77. */
  78. int netarpme
  79.   (
  80.     char *s
  81.   )
  82.   {
  83.     if (EtherNet) {    
  84.         reqarp(s);
  85.     }
  86.     return(0);
  87.   }
  88.  
  89. /*
  90.  * ATdlayersend (ptr, size)
  91.  *  send size from ptr out the AT port
  92.  *    All outgoing conversions done here ! 
  93.  */
  94. ATdlayersend(ptr,size)            /*MAC: replaced */
  95. ARPKT *ptr;
  96. unsigned size;
  97. {
  98.     char *todata;
  99.     AddrBlock *destptr;
  100.     int ptype, tsize;
  101.     char wds[14], header[18];
  102.     MPPParamBlock pb;
  103.     static ATARPKT arpbuffer;
  104.  
  105.     int err;
  106.  
  107.     ptype = AIP;                                    /* Default to sending an IP Packet */
  108.  
  109.     pb.DDPsocket=IPSock;                            /* Sorry.... */
  110.     pb.DDPchecksumFlag=FALSE;                        /* who knows why, but we don't */
  111.     pb.DDPwdsPointer=wds;                            /* Here we go..... */
  112.     pb.MPPcsCode=246;                                /* We want to send a packet */
  113.  
  114.     todata=(char *)ptr + sizeof(DLAYER);            /* Point at the real data... */
  115.     (uint8 *) destptr = &ptr->d.dest[0];            /* Where is the destination ? */
  116.     tsize = size- sizeof(DLAYER);                    /* Get transmission size */
  117.  
  118.     if (!memcmp( destptr, bseed, 6))                    /* Watch for Broadcasts */
  119.         movmem(ATbseed, destptr, 4);
  120.  
  121.     if (ptr->d.type == EARP) {
  122.         ptype=AARP;                                        /* Arp packet */
  123.         arpbuffer.hrd = 3;                                /* ATalk */
  124.         arpbuffer.hln = 4;
  125.         arpbuffer.pln = 4;
  126.         arpbuffer.pro = ptr->pro;
  127.         arpbuffer.op  = ptr->op;
  128.         movmem( &ptr->sha[0], &arpbuffer.sha[0], 4);    /* Move addresses */
  129.         movmem( &ptr->spa[0], &arpbuffer.spa[0], 4);
  130.         movmem( &ptr->tha[0], &arpbuffer.tha[0], 4);
  131.         movmem( &ptr->tpa[0], &arpbuffer.tpa[0], 4);
  132.         todata= (char *) &arpbuffer;
  133.         todata+= sizeof(ATdlayer);                        /* New address to go from */
  134.         tsize = sizeof(ATARPKT)- sizeof(ATdlayer);
  135.         }
  136.         
  137.   /* note incorrect definition of BuildDDPwds in AppleTalk.h:
  138.     the destAddress arg is an AddrBlock, not a pointer to one! */
  139.     BuildDDPwds( wds, header, todata, *(AddrBlock *) destptr, ptype, tsize );    /* BYU 2.4.16 MPW */
  140.     
  141.     err=PWriteDDP( &pb, FALSE);
  142.     if (err!=0) putln("DDPW Error = %d");
  143. #ifdef SENDNOTICE    
  144.     else putln("send");
  145. #endif
  146.     return(err);
  147.     
  148. }
  149.  
  150. /*****************************************************************
  151.  *     E  T  H  E  R  N  E  T   /   E  T  H  E  R  T  A  L  K    *
  152.  *****************************************************************/
  153.  
  154. struct ETHERWDSstruct {
  155.     short len1;
  156.     DLAYER *ptr1;
  157.     short len2;
  158.     DLAYER *ptr2;
  159.     short term;
  160.     } EWDS;
  161.  
  162. int ETdlayersend            /*MAC: replaced */
  163.   (
  164.     DLAYER *ptr,
  165.     unsigned size
  166.   )
  167. {
  168.     EtParam ET;
  169.     int err;
  170.  
  171.     err = 0;
  172.     EWDS.len1=size;                    /* Could it would it in a WDS? */
  173.     EWDS.ptr1=ptr;
  174.     EWDS.len2=0;
  175.  
  176.     ET.address = &EWDS;
  177.  
  178.     /* err = */ ETcall( &ET, EWrite);
  179.     if (/* err != */ 0)
  180.         putln("ETW Error = %d");
  181. #ifdef SENDNOTICE    
  182.     else putln("send");
  183. #endif
  184.     return(err);
  185.     
  186. }
  187.  
  188. /**********************************************************************/
  189. /*   ETdemux    - Ethernet Version of DEMUX routine
  190. *
  191. *  returns the number of packets demuxed
  192. */
  193.  
  194. int ETdemux
  195.   (
  196.     int all
  197.   )
  198.     {
  199.     unsigned getcode;
  200.     int nmuxed,sizered;
  201.     typedef struct peek {
  202.         unsigned short count;
  203.         unsigned char dest[DADDLEN],
  204.             me[DADDLEN];
  205.         unsigned short type;
  206.     } Dpeek;
  207.  
  208.     Dpeek *firstlook;
  209.  
  210.     nmuxed = 0;
  211.  
  212.     do {                                    /* while all flag is on */
  213.  
  214.         if (bufinfo.bufbig > 0) { /*MAC*/
  215.  
  216.             nmuxed++;
  217.             firstlook = (Dpeek *)(bufinfo.bufread);     /*MAC where packet is */
  218.  
  219.             getcode = firstlook->type;            /* where does it belong? */
  220.  
  221.             switch (getcode) {                    /* what to do with it? */
  222.                 case EARP:
  223.                 case ERARP:
  224.                     arpinterpret((ARPKT *) firstlook->dest); /* handle [R]ARP packet */
  225.                     break;
  226.                 case EIP: 
  227.                     ipinterpret((IPKT *) firstlook->dest,NOT_FROM_SLIP);    /* BYU 2.4.15 - handle IP packet */
  228.                     break;
  229.                 default:
  230.                     break;
  231.             }
  232.  
  233.             sizered=(firstlook->count+sizeof(Dpeek));
  234.             bufinfo.bufbig-=sizered;
  235.             
  236. #ifdef DEBUGONLY
  237.             {
  238.             char s[60];
  239.             sprintf(s,"buf: %d, pkt: %d",bufinfo.bufbig,sizered);
  240.             putln(s);
  241.             }
  242. #endif
  243.             bufinfo.bufread+=(sizered+1) & 0xFFFE;
  244.  
  245.             if (bufinfo.bufbig<0) bufinfo.bufbig=0;
  246.             if (bufinfo.bufread>bufinfo.bufend) bufinfo.bufread=bufinfo.buforg;
  247.             
  248.         }
  249.         else {
  250.  
  251.             if (ETdeafreset()) {
  252.                 /* clear deaf flag, buffer overrun */
  253.                 putln("warning: buffer deaf, restarting");
  254.                 }
  255.  
  256.             all = 0;
  257.             }
  258.  
  259.  
  260.     } while (all);            /* should we look for more to deal with? */
  261.  
  262.  
  263.     return(nmuxed);          /* no packets anymore */
  264.  
  265. }
  266.  
  267. /************************************************************************/
  268. /*  dlayersend
  269. *
  270. *  usage:   err = dlayersend(ptr,size)
  271. *      err = 0 for successful, non-zero error code otherwise
  272. *      ptr is to a dlayer packet header
  273. *      size is the number of bytes total
  274. *
  275. *  This particular dlayer routine is for Ethernet.  It will have to be
  276. *  replaced for any other dlayer.
  277. *
  278. *  Assumes that dlayer-dependent initializations were made in protinit.c
  279. *  to help efficiency.
  280. *
  281. *  Ethernet addresses are resolved at higher levels because they will only
  282. *  need to be resolved once per logical connection, instead of once per
  283. *  packet.  Not too layer-like, but hopefully modular.
  284. *
  285. */
  286.  
  287. int dlayersend
  288.   (
  289.     DLAYER *ptr,
  290.     int size,                /* BYU 2.4.15 */
  291.     int connectionType        /* BYU 2.4.15 */
  292.   )
  293. {
  294.     if (connectionType > 0)                                /* BYU 2.4.15 */
  295.         SLIPdlayersend((unsigned char *) ptr,size);        /* BYU 2.4.15 */
  296.     else if (EtherNet== 0)                                /* BYU 2.4.15 */
  297.         return( ATdlayersend(ptr,size));
  298.     else if (EtherNet==-2)                                /* BYU 2.4.15 - added "else" */
  299.         return( FNdlayersend(ptr,size));
  300.     else
  301.         return( ETdlayersend(ptr,size));
  302. }
  303.  
  304. int ATdemux
  305.   (
  306.     int all
  307.   )
  308. {
  309.     unsigned getcode;
  310.     int nmuxed,sizered, count;
  311.     ATARPKT *firstlook;
  312.     static ARPKT arpbuffer;
  313.     nmuxed = 0;
  314.  
  315.     do {                                    /* while all flag is on */
  316.  
  317.         if (bufinfo.bufbig > 0) {
  318.             nmuxed++;
  319.             firstlook = (ATARPKT *)(bufinfo.bufread);     /*MAC where packet is */
  320.  
  321.             getcode = firstlook->d.type;            /* where does it belong? */
  322.             count = firstlook->d.count;
  323.             movmem( firstlook->d.dest, arpbuffer.d.dest,4);
  324.             movmem( firstlook->d.me,   arpbuffer.d.me  ,4);
  325.             
  326.             switch (getcode) {                    /* what to do with it? */
  327.                 case AARP:
  328.                     arpbuffer.d.type=EARP;                            /* Arp packet */
  329.                     arpbuffer.hrd = 1;                                /* Enet */
  330.                     arpbuffer.hln = 6;
  331.                     arpbuffer.pln = 4;
  332.                     arpbuffer.pro = firstlook->pro;
  333.                     arpbuffer.op  = firstlook->op;
  334.                     movmem( &firstlook->sha[0], &arpbuffer.sha[0], 4);        /* Move addresses */
  335.                     movmem( &firstlook->spa[0], &arpbuffer.spa[0], 4);
  336.                     movmem( &firstlook->tha[0], &arpbuffer.tha[0], 4);
  337.                     movmem( &firstlook->tpa[0], &arpbuffer.tpa[0], 4);
  338.                     arpinterpret(&arpbuffer);    /* handle ARP packet */
  339.                     break;
  340.                 case AIP:
  341.                     arpbuffer.d.type=EIP;
  342.                     movmem( &arpbuffer, firstlook, sizeof(DLAYER) );
  343.                     ipinterpret((IPKT *) firstlook,NOT_FROM_SLIP);        /* BYU 2.4.15 */
  344.                     break;
  345.                 default:
  346.                     break;
  347.             }
  348.  
  349.             sizered = count + sizeof(DLAYER);
  350.             bufinfo.bufbig-=sizered;
  351.             bufinfo.bufread+=(sizered+1) & 0xFFFE;
  352.  
  353.             if (bufinfo.bufbig<0) bufinfo.bufbig=0;
  354.             if (bufinfo.bufread>bufinfo.bufend) bufinfo.bufread=bufinfo.buforg;
  355.  
  356.         }
  357.         else 
  358.             all = 0;
  359.  
  360.  
  361.     } while (all);            /* should we look for more to deal with? */
  362.  
  363.  
  364.     return(nmuxed);          /* no packets anymore */
  365.  
  366. }
  367.  
  368. /**********************************************************************/
  369. /*   demux
  370. *      find the packets in the buffer, determine their lowest level
  371. *  packet type and call the correct interpretation routines
  372. *
  373. *  the 'all' parameter tells demux whether it should attempt to empty
  374. *  the input packet buffer or return after the first packet is dealt with.
  375. *
  376. *  returns the number of packets demuxed
  377. */
  378.  
  379. int demux
  380.   (
  381.     int all
  382.   )
  383. {
  384.     int total = 0;                        /* BYU 2.4.15 */
  385.     if (slip_connection)                /* BYU 2.4.15 */
  386.         total += SLIPdemux(all);        /* BYU 2.4.15 */
  387.     if (EtherNet==0)
  388.         total += ATdemux(all);            /* BYU 2.4.15 */
  389.     if (EtherNet==-2)
  390.         total += FNdemux(all);            /* BYU 2.4.15 */
  391.     else if (EtherNet > -99)             /* BYU 2.4.16 */
  392.         total += ETdemux(all);            /* BYU 2.4.15 */
  393.     return(total);                        /* BYU 2.4.15 */
  394. }
  395.  
  396. initbuffer()
  397.     {
  398.     (Ptr) raw=NewPtr(17000);        /* Added by GBP */
  399.     if (raw==NULL) return(-1);
  400.     bufinfo.bufpt = bufinfo.bufread = bufinfo.buforg
  401.                                          = raw;    /*  start at the beginning */
  402.  
  403.     bufinfo.bufend = raw+14500;            /* leave 2K breathing room, required */
  404.     bufinfo.buflim = 12000;             /* another 2K breathing room */
  405.     bufinfo.bufbig = 0;                    /* Added by GBP */
  406.  
  407.     if (EtherNet==0)
  408.         InitBufPtr(&bufinfo);
  409.     else if (EtherNet != -2) 
  410.         ETInitBufPtr( &bufinfo);
  411.     return(0);
  412. }
  413.  
  414. /***************************************************************************/
  415. /* dlayerinit
  416. *  Do machine dependent initializations of whatever hardware we have
  417. *  (happens to be 3com ethernet board here ) 
  418. */
  419. int dlayerinit
  420.   (
  421.     void
  422.   )
  423. {
  424.  
  425.     if (initbuffer())
  426.         return(-1);
  427.     if (EtherNet==0) 
  428.         return(atopen());
  429.     if (EtherNet==-2)
  430.         return(FNopen());
  431.     else if (EtherNet > -99)    /* BYU 2.4.15 */
  432.         return(ETopen());    /* Pass the Type along */
  433.     else if (EtherNet == -99)    /* BYU 2.4.15 MPW */
  434.         return(0);                /* BYU 2.4.15 MPW */
  435.     return(-1);                    /* BYU 2.4.15 MPW */
  436. }
  437.  
  438. int dlayershut
  439.   (
  440.     void
  441.   )
  442. {
  443.     if (EtherNet==0)
  444.         return(atclose());
  445.     if (EtherNet==-2)
  446.         return(FNclose());
  447.     else if (EtherNet > -100)    /* BYU 2.4.15 */
  448.         return(ETclose());
  449. }
  450.  
  451.